<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>角色管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../css/public.css" media="all">
	</head>
	<body>
		<div class="layuimini-container">
			<div class="layuimini-main">
				<fieldset class="layui-elem-field layuimini-search">
					<legend>搜索信息</legend>
					<div style="margin: 10px 10px 10px 10px">
						<form class="layui-form layui-form-pane" action="" id="searchFrm">
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">角色名称</label>
									<div class="layui-input-inline">
										<input type="text" name="name" autocomplete="off" class="layui-input" placeholder="请输入角色名称">
									</div>
								</div>
								<div class="layui-inline" style="text-align: center;">
									<a class="layui-btn" lay-submit="" lay-filter="data-search-btn">
										<i class="layui-icon layui-icon-search"></i>搜索</a>
									<a class="layui-btn layui-btn-warm" lay-submit="" lay-filter="data-reset-btn">
										<i class="layui-icon layui-icon-refresh"></i>重置</a>
								</div>
							</div>
						</form>
					</div>
				</fieldset>


				<!--表格开始-->
				<table class="layui-hide" id="roleTable" lay-filter="roleFilter"></table>
				<div style="display: none;" id="roleToolBar">
					<a class="layui-btn layui-btn-sm btn-add btn-add-role" lay-event="addRole">
						<i class="layui-icon layui-icon-addition"></i>添加角色</a>
				</div>
				<script type="text/html" id="availableTpl">
					<!-- 这里的 checked 的状态只是演示 -->
				  <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableSwitch" {{ d.available == 1 ? 'checked' : '' }}>
				</script>
				<div style="display: none;" id="roleBar">
					<a class="layui-btn layui-btn-xs btn-update btn-update-role" lay-event="update">编辑</a>
					<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete btn-delete btn-delete-role" lay-event="delete">删除</a>
					<a class="layui-btn layui-btn-xs layui-btn-warm btn-asign btn-asign-role" lay-event="asign-permission">分配权限</a>
				</div>
				<!--表格结束-->

				<!--添加和修改的弹层开始-->
				<div id="addOrUpdateDiv" style="display: none; padding: 0.3125rem;">
					<form method="post" class="layui-form layui-form-pane" lay-filter="dataFilter" id="dataFrm">
						<div class="layui-form-item">
							<label class="layui-form-label">角色名称</label>
							<div class="layui-input-block">
								<input type="hidden" name="id" />
								<input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入角色名称" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">备注</label>
							<div class="layui-input-block">
								<input type="text" name="remark" autocomplete="off" placeholder="请输入备注" class="layui-input">
							</div>
						</div>

						<div class=class="layui-form-item" style="text-align: center;">
							<a class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit">
								<i class="layui-icon layui-icon-release"></i>立即提交</a>
							<a class="layui-btn layui-btn-warm" lay-submit="" lay-filter="dataFrm-reset-btn">
								<i class="layui-icon layui-icon-refresh"></i>重置</a>
						</div>
					</form>
				</div>
				<!--添加和修改的弹层结束-->

				<!--分配菜单权限的弹层开始-->
				<div id="asignPermissionDiv" style="display: none; padding: 0.3125rem;">
					<table class="layui-hide" id="menuSelectTable" lay-filter="permission"></table>
				</div>
				<!--分配菜单权限弹层结束-->
			</div>
		</div>

		<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
		<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
		<script src="../../../lib/common/selectLayer.js" charset="utf-8"></script>
		<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
		<script src="../../../lib/common/common.js" charset="utf-8"></script>
		<script>
			layui.use(['form', 'table', 'laydate', 'layer', 'treeTable'], function() {
				var $ = layui.jquery,
					form = layui.form,
					table = layui.table,
					layer = layui.layer,
					treeTable = layui.treeTable;

				/**
				 * 加载 数据
				 */
				var tableIns = table.render({
					elem: '#roleTable',
					url: api + 'role/loadAllRole',
					title: '角色数据表',
					toolbar: '#roleToolBar',
					height: 'full-220',
					cols: [
						[{
								field: 'name',
								title: '角色名称',
								align: "center"
							},
							{
								field: 'remark',
								title: '备注',
								align: "center"
							},
							{
								field: 'available',
								title: '是否可用',
								align: "center",
								templet: '#availableTpl'
							},
							{
								field: 'createtime',
								title: '创建时间',
								sort: true,
								align: "center"
							},
							{
								title: '操作',
								templet: '#roleBar',
								fixed: "right",
								align: "center",
								width: 220
							}
						]
					],
					done: function (data, curr, count) {
					                //不是第一页时，如果当前返回的数据为0那么就返回上一页
					                if (data.data.length === 0 && curr !== 1) {
					                    tableIns.reload({
					                        page: {
					                            curr: curr - 1
					                        }
					                    })
					                }
					                /*//当是第一页时，如果当前返回的数据为0并且当前为第一页  给一个空的查询条件，并给curr赋初值为1
					                if (data.data.length==0 && curr==1){
					                    tableIns.reload({
					                        where: "",
					                        page:{
					                            curr:1
					                        }
					                    })
					                }*/
					            },
					// limits: [10, 15, 20, 25, 50, 100],
					// limit: 10,
					page: true
				});

				/**
				 * 监听搜索操作
				 */
				form.on('submit(data-search-btn)', function(data) {
					var params = $("#searchFrm").serialize();
					//执行搜索重载
					table.reload('roleTable', {
						page: {
							curr: 1
						},
						url: api + 'role/loadAllRole?' + params
					});
					return false;
				});

				/**
				 * 监听重置操作
				 */
				form.on('submit(data-reset-btn)', function(data) {
					$("#searchFrm")[0].reset(); // 清空搜索信息框中的数据
					//执行搜索重载
					table.reload('roleTable', {
						page: {
							curr: 1
						},
						url: api + 'role/loadAllRole'
					});
					return false;
				});

				form.on('submit(dataFrm-reset-btn)', function(data) {
					$("#dataFrm")[0].reset(); // 清空表格信息框中的数据
					return false;
				});

				/**
				 * 监听表头的事件
				 */
				table.on('toolbar(roleFilter)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'batchdelete':
							batchedelete();
							break;
						case 'addRole':
							addRole();
							break;
					};
				});

				/**
				 * 监听行数据的事件
				 */
				table.on('tool(roleFilter)', function(obj) {
					var data = obj.data; // 获得当前行数据
					if (obj.event == 'delete') {
						layer.confirm('真的删除行么？', {
							icon: 3,
							title: '提示'
						}, function(index) {
							$.post(api + 'role/queryMenuIdsByRid', {
									rid: data.id
								},
								function(d) {
									if (!d.length == 0) {
										layer.msg("删除失败！请先移除该角色的权限");
									} else {
										$.post(api + 'role/deleteRole', {
												id: data.id
											},
											function(res) {
												if (res.code == 200) {
													tableIns.reload();
												}
												layer.msg(res.msg);
											})
									}
								})

							layer.close(index);
						});
					} else if (obj.event == 'update') {
						updateRole(data);
					} else if (obj.event == 'asign-permission') {
						asignPermission(data);
					}
				});

				//监听按钮操作
				form.on('switch(availableSwitch)', function(obj) {
					$.post(api + 'role/updateRole', {
							id: this.value,
							available: obj.elem.checked ? 1 : 0
						},
						function(res) {})
				});

				/**
				 * 打开公告的添加弹出层
				 */
				var mainIndex;
				var url; // 动态的url
				function addRole() {
					mainIndex = layer.open({
						type: 1,
						title: '添加角色',
						area: ['500px'],
						content: $("#addOrUpdateDiv"),
						success: function(index) {
							url = api + "role/addRole";
							$("#dataFrm")[0].reset();
						}
					})
				}

				/**
				 * 打开公告的编辑弹出层
				 */
				function updateRole(data) {
					mainIndex = layer.open({
						type: 1,
						title: '修改角色',
						content: $("#addOrUpdateDiv"),
						area: ['500px'],
						success: function(index) {
							id = data.id;
							title = data.title;
							url = api + "role/updateRole";
							// 装载数据表格 	
							form.val("dataFilter", data);
						}
					})
				}

				/**
				 * 分配权限
				 */
				function asignPermission(data_table) {
					var menu_tableIns;
					mainIndex = layer.open({
						type: 1,
						title: '给角色分配菜单权限',
						content: $("#asignPermissionDiv"),
						area: ['600px', '400px'],
						btn: ['<span class="layui-icon layui-icon-ok"></span>确定'],
						btnAlign: 'c',
						yes: function(index) {
							var m = menu_tableIns.checkStatus();
							var rid = data_table.id;
							var params = "rid=" + rid;
							$.each(m, function(i, item) {
								params += "&mids=" + item.id;
							})
							$.post(api + 'role/saveRoleMenu', params, function(res) {
								layer.msg(res.msg);
							})
						},
						success: function(index) {
							menu_tableIns = treeTable.render({
								elem: '#menuSelectTable',
								tree: {
									iconIndex: 1, // 折叠图标显示在第几列
									idName: 'id', // 自定义id字段的名称
									pidName: 'pid', // 自定义标识是否还有子节点的字段名称
									haveChildName: 'haveChild', // 自定义标识是否还有子节点的字段名称
									isPidData: true // 是否是pid形式数据
								},
								cols: [{
										type: "checkbox"
									},
									{
										field: 'title',
										title: '菜单标题'
									},
									{
										field: 'type',
										title: '类型',
										align: "center",
										templet: function(data) {
											if (data.type == 'topmenu') {
												return '<span class="layui-badge layui-bg-gray">顶部菜单</span>';
											}
											if (data.type == 'leftmenu') {
												return '<span class="layui-badge layui-bg-blue">左侧菜单</span>';
											} else {
												return '<span class="layui-badge-rim">权限</span>';
											}
										}
									},
								],
								reqData: function(data, callback) {
									if (data == undefined) {
										// 在这里写ajax请求，通过callback方法回调数据
										$.get(api + 'menu/loadAllMenu?available=1', function(res) {
											callback(res.data); // 参数是数组类型
											// 选中已选择的菜单
											$.get(api + "role/queryMenuIdsByRid", {
												rid: data_table.id
											}, function(rs) {
												var x = new Array();
												$.each(res.data, function(i, i_1) {
													$.each(rs, function(j, j_1) {
														if (i_1.id == j_1) {
															if (!i_1.children) {
																x.push(j_1);
															}
														}
													})
												})
												menu_tableIns.setChecked(x); // 设置选中数据
											});
										});
									} else {
										callback([]); // 回调一个空数据去渲染表格
									}

								},
								page: false
							});
						}
					})
				}


				/**
				 * 提交数据
				 */
				form.on('submit(doSubmit)', function(data) {
					var params = $("#dataFrm").serialize();
					$.post(url, params, function(res) {
						layer.msg(res.msg);
						if (res.code == 200) {
							tableIns.reload();
						}
						layer.close(mainIndex);
						layer.close(menu_tableIns);
					})
					return false;
				});

			});
		</script>
		<script>

		</script>

	</body>
</html>
